package com.platform.controller;

import com.platform.entity.InviteEntity;
import com.platform.service.InviteService;
import com.platform.service.SysDeptService;

import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

import com.platform.utils.SmsUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 邀请码controller
 *
 * @author yanzhiwen
 * @email 2072482130@qq.com
 * @date 2018-11-21 05:59
 */
@RestController
@RequestMapping("invite")
public class InviteController extends AbstractController{
    @Autowired
    private InviteService inviteService;
    @Autowired
    private SysDeptService sysDeptService;

    @RequestMapping("/list")
    @RequiresPermissions("invite:list")
    public R list(@RequestParam Map<String, Object> params){
        Query query = new Query(params);

        //中大团队扩展，只可获取商家自己的商品
        query.put("rootDeptId", getRootDept().getDeptId());

        List<InviteEntity> queryList = inviteService.queryList(query);
        int queryTotal = inviteService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(queryList, queryTotal, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/info/{id}")
    @RequiresPermissions("invite:info")
    public R info(@PathVariable("id") Integer id) {
        InviteEntity inviteEntity = inviteService.queryObject(id);

        return R.ok().put("invite", inviteEntity);
    }

    @RequestMapping("/save")
    @RequiresPermissions("invite:save")
    public R save(@RequestBody InviteEntity inviteEntity) throws Exception {
        String deptId = getDeptId().toString();
        inviteEntity.setDeptId(deptId);
        inviteEntity.setIsUsed('0');
        inviteEntity.setUseCode('0');
        inviteService.save(inviteEntity);
        logger.info("手机号为：" + inviteEntity.getMobile());
        SmsUtil t = new SmsUtil();
        t.sendInviteCodeSms(inviteEntity.getMobile(),inviteEntity.getInvitation());
        return R.ok();
    }

    @RequestMapping("/update")
    @RequiresPermissions("invite:update")
    public R update(@RequestBody InviteEntity inviteEntity) {
        inviteService.update(inviteEntity);

        return R.ok();
    }

    @RequestMapping("/delete")
    @RequiresPermissions("invite:delete")
    public R delete(@RequestBody Integer id) {
        inviteService.delete(id);

        return R.ok();
    }
}
